package archimede;

import it.sauronsoftware.ftp4j.FTPAbortedException;
import it.sauronsoftware.ftp4j.FTPClient;
import it.sauronsoftware.ftp4j.FTPDataTransferException;
import it.sauronsoftware.ftp4j.FTPDataTransferListener;
import it.sauronsoftware.ftp4j.FTPException;
import it.sauronsoftware.ftp4j.FTPFile;
import it.sauronsoftware.ftp4j.FTPIllegalReplyException;

import java.awt.Dimension;
import java.io.File;
import java.io.IOException;
import java.util.Properties;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JProgressBar;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import net.miginfocom.swing.MigLayout;

public class Upload implements Runnable {

  private String nameProject;
  private String password;
  private String username;
  private String path;
  private StartingWindow st;

  public Upload(String nameproject, String password, String username,
      String path, StartingWindow st) {
    this.nameProject = nameproject;
    this.password = password;
    this.username = username;
    this.path = path;
    this.st = st;
  }

  public void start(String password, String username, String path)
      throws IllegalStateException, IOException, FTPIllegalReplyException,
      FTPException, FTPDataTransferException, FTPAbortedException {

  }

  @Override
  public void run() {
    MyTransferListener transfer = new MyTransferListener();
    try {
      FTPClient client = new FTPClient();
      // connessione
      client.setPassive(true);
      client.connect("eureka.archimedegroup.eu");
      // login
      client.login(username, password);

      client.changeDirectory("ProgettoArchimede");
      client.changeDirectory("AMMINISTRATORE");
      try {
        client.changeDirectory(nameProject);
        try {
          client.changeDirectory("eureka");
          FTPFile[] fileToDelete = client.list();
          for (int i = 0; i < fileToDelete.length; i++)
            client.deleteFile(fileToDelete[i].getName());
        } catch (Exception e) {
          e.printStackTrace();
        }
      } catch (Exception e) {
        client.createDirectory(nameProject);
        client.changeDirectory(nameProject);
        client.createDirectory("eureka");
        client.createDirectory("extra");
        client.changeDirectory("eureka");
      }

      // cartella di progetto da caricare solo EUREKA
      File dir = new File(path);
      File[] fileDir = dir.listFiles();

      transfer.setStatus("Progetto EUREKA");
      for (int i = 0; i < fileDir.length; i++) {
        if (fileDir[i].isFile())
          client.upload(fileDir[i], transfer);
      }

      // carico tutti gli extra
      Properties pc = System.getProperties();

      client.changeDirectoryUp();
      client.changeDirectory("extra");
      try {
        client.createDirectory("cliente_collaboratore");
      } catch (Exception e) {
      }
      client.changeDirectory("cliente_collaboratore");
      dir = new File(path + pc.getProperty("file.separator") + "extra"
          + pc.getProperty("file.separator") + "cliente_collaboratore");
      fileDir = dir.listFiles();
      transfer.setStatus("Dati collaboratore");
      for (int i = 0; i < fileDir.length; i++) {
        if (fileDir[i].isFile())
          client.upload(fileDir[i], transfer);
      }
      client.changeDirectoryUp();

      try {
        client.createDirectory("documenti");
      } catch (Exception e) {
      }
      client.changeDirectory("documenti");
      dir = new File(path + pc.getProperty("file.separator") + "extra"
          + pc.getProperty("file.separator") + "documenti");
      fileDir = dir.listFiles();
      transfer.setStatus("Documenti");
      for (int i = 0; i < fileDir.length; i++) {
        if (fileDir[i].isFile())
          client.upload(fileDir[i], transfer);
      }
      client.changeDirectoryUp();

      try {
        client.createDirectory("foto");
        client.createDirectory("termografia");
        client.changeDirectory("foto");
        client.createDirectory("punti_misurazione");
        client.createDirectory("punti_critici");
        client.createDirectory("tensione_verticale");
        client.createDirectory("tensione_orizzontale");
        client.createDirectory("premesse");
        client.createDirectory("sali");
        client.createDirectory("pianta");
      } catch (Exception e) {
        client.changeDirectory("foto");
      }

      client.changeDirectory("punti_misurazione");
      dir = new File(path + pc.getProperty("file.separator") + "extra"
          + pc.getProperty("file.separator") + "foto"
          + pc.getProperty("file.separator") + "punti_misurazione");
      fileDir = dir.listFiles();
      transfer.setStatus("Foto");
      for (int i = 0; i < fileDir.length; i++) {
        if (fileDir[i].isFile())
          client.upload(fileDir[i], transfer);
      }
      client.changeDirectoryUp();

      client.changeDirectory("punti_critici");
      dir = new File(path + pc.getProperty("file.separator") + "extra"
          + pc.getProperty("file.separator") + "foto"
          + pc.getProperty("file.separator") + "punti_critici");
      fileDir = dir.listFiles();
      for (int i = 0; i < fileDir.length; i++) {
        if (fileDir[i].isFile())
          client.upload(fileDir[i], transfer);
      }
      client.changeDirectoryUp();

      client.changeDirectory("tensione_verticale");
      dir = new File(path + pc.getProperty("file.separator") + "extra"
          + pc.getProperty("file.separator") + "foto"
          + pc.getProperty("file.separator") + "tensione_verticale");
      fileDir = dir.listFiles();
      for (int i = 0; i < fileDir.length; i++) {
        if (fileDir[i].isFile())
          client.upload(fileDir[i], transfer);
      }
      client.changeDirectoryUp();

      client.changeDirectory("tensione_orizzontale");
      dir = new File(path + pc.getProperty("file.separator") + "extra"
          + pc.getProperty("file.separator") + "foto"
          + pc.getProperty("file.separator") + "tensione_orizzontale");
      fileDir = dir.listFiles();
      for (int i = 0; i < fileDir.length; i++) {
        if (fileDir[i].isFile())
          client.upload(fileDir[i], transfer);
      }
      client.changeDirectoryUp();

      client.changeDirectory("premesse");
      dir = new File(path + pc.getProperty("file.separator") + "extra"
          + pc.getProperty("file.separator") + "foto"
          + pc.getProperty("file.separator") + "premesse");
      fileDir = dir.listFiles();
      for (int i = 0; i < fileDir.length; i++) {
        if (fileDir[i].isFile())
          client.upload(fileDir[i], transfer);
      }
      client.changeDirectoryUp();

      client.changeDirectory("sali");
      dir = new File(path + pc.getProperty("file.separator") + "extra"
          + pc.getProperty("file.separator") + "foto"
          + pc.getProperty("file.separator") + "sali");
      fileDir = dir.listFiles();
      for (int i = 0; i < fileDir.length; i++) {
        if (fileDir[i].isFile())
          client.upload(fileDir[i], transfer);
      }
      client.changeDirectoryUp();

      client.changeDirectory("pianta");
      dir = new File(path + pc.getProperty("file.separator") + "extra"
          + pc.getProperty("file.separator") + "foto"
          + pc.getProperty("file.separator") + "pianta");
      fileDir = dir.listFiles();
      for (int i = 0; i < fileDir.length; i++) {
        if (fileDir[i].isFile())
          client.upload(fileDir[i], transfer);
      }
      client.changeDirectoryUp();

      client.changeDirectoryUp();
      client.changeDirectory("termografia");

      try {
        client.createDirectory("premesse");
        client.createDirectory("punti_misurazione");
        client.createDirectory("punti_critici");
      } catch (Exception e) {
      }

      client.changeDirectory("premesse");
      dir = new File(path + pc.getProperty("file.separator") + "extra"
          + pc.getProperty("file.separator") + "termografia"
          + pc.getProperty("file.separator") + "premesse");
      fileDir = dir.listFiles();
      transfer.setStatus("Termografia");
      for (int i = 0; i < fileDir.length; i++) {
        if (fileDir[i].isFile())
          client.upload(fileDir[i], transfer);
      }
      client.changeDirectoryUp();

      client.changeDirectory("punti_misurazione");
      dir = new File(path + pc.getProperty("file.separator") + "extra"
          + pc.getProperty("file.separator") + "termografia"
          + pc.getProperty("file.separator") + "punti_misurazione");
      fileDir = dir.listFiles();
      for (int i = 0; i < fileDir.length; i++) {
        if (fileDir[i].isFile())
          client.upload(fileDir[i], transfer);
      }
      client.changeDirectoryUp();

      client.changeDirectory("punti_critici");
      dir = new File(path + pc.getProperty("file.separator") + "extra"
          + pc.getProperty("file.separator") + "termografia"
          + pc.getProperty("file.separator") + "punti_critici");
      fileDir = dir.listFiles();
      for (int i = 0; i < fileDir.length; i++) {
        if (fileDir[i].isFile())
          client.upload(fileDir[i], transfer);
      }
      client.changeDirectoryUp();

      // logout
      // client.logout();
      // libero il server
      client.disconnect(true);
      transfer.hideWindow();

    } catch (IllegalStateException e1) {
      transfer.hideWindow();
      e1.printStackTrace();
    } catch (IOException e1) {
      transfer.hideWindow();
      e1.printStackTrace();
    } catch (FTPIllegalReplyException e1) {
      transfer.hideWindow();
      JOptionPane.showMessageDialog(null, "Errore nella connessione , riprova");
      e1.printStackTrace();
    } catch (FTPException e1) {
      transfer.hideWindow();
      JOptionPane.showMessageDialog(null, "Errore nella connessione , riprova");
      e1.printStackTrace();
    } catch (FTPDataTransferException e) {
      transfer.hideWindow();
      JOptionPane.showMessageDialog(null, "Errore nella connessione , riprova");
      e.printStackTrace();
    } catch (FTPAbortedException e) {
      transfer.hideWindow();
      JOptionPane.showMessageDialog(null, "Errore nella connessione , riprova");
      e.printStackTrace();
    }

  }

  public class MyTransferListener extends JFrame implements
      FTPDataTransferListener {

    private JProgressBar bar;

    public MyTransferListener() {
      super("Upload");
      try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
      } catch (UnsupportedLookAndFeelException localUnsupportedLookAndFeelException) {
      } catch (ClassNotFoundException localClassNotFoundException) {
      } catch (InstantiationException localInstantiationException) {
      } catch (IllegalAccessException localIllegalAccessException) {
      }
      bar = new JProgressBar(0, 100);
      bar.setIndeterminate(true);
      bar.setPreferredSize(new Dimension(200, 40));
      bar.setStringPainted(true);
      setLayout(new MigLayout());
      add(new JLabel("Attendere upload in corso"), "wrap");

      add(bar);
      pack();
      setLocation(st.getWidth() / 2 - getWidth() / 2, st.getHeight() / 2
          - getHeight() / 2);
      setVisible(true);
    }

    public void setStatus(String status) {
      bar.setString(status);
    }

    public void hideWindow() {
      setVisible(false);
    }

    public void started() {
    }

    public void transferred(int length) {
    }

    public void completed() {

    }

    public void aborted() {
    }

    public void failed() {
    }

  }
}
